﻿namespace AnyOffice.WebControls
{
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    internal class DataListItemTemplate : ITemplate
    {
        private string _imageUrl;
        private string[] itemCols;
        private string strUlr;

        public DataListItemTemplate(string[] colNames, string imageUrl, string Url)
        {
            this._imageUrl = imageUrl;
            this.itemCols = colNames;
            this.strUlr = Url;
        }

        public void InstantiateIn(Control container)
        {
            WebControl child = new WebControl(HtmlTextWriterTag.A);
            Image image = new Image();
            image.ID = "itemIco";
            image.DataBinding += new EventHandler(this.OnImageDataBinding);
            child.Controls.Add(image);
            for (int i = 0; i < this.itemCols.Length; i++)
            {
                Label label = new Label();
                label.ID = this.itemCols[i];
                label.Width = Unit.Pixel(120);
                label.DataBinding += new EventHandler(this.OnDataBinding);
                child.Controls.Add(label);
            }
            child.DataBinding += new EventHandler(this.OnADataBinding);
            container.Controls.Add(child);
        }

        public void OnADataBinding(object sender, EventArgs e)
        {
            string str = DataBinder.Eval(((WebControl) sender).NamingContainer, "DataItem." + this.strUlr.Trim()).ToString();
            ((WebControl) sender).Attributes.Add("href", str);
        }

        public void OnDataBinding(object sender, EventArgs e)
        {
            object obj2 = DataBinder.Eval(((Label) sender).NamingContainer, "DataItem." + ((Label) sender).ID);
            ((Label) sender).Text = (obj2 == null) ? "" : obj2.ToString();
        }

        public void OnImageDataBinding(object sender, EventArgs e)
        {
            ((Image) sender).ImageUrl = this._imageUrl;
        }
    }
}

